#coding:utf-8
"""
*   TG_Coder : Dzlua
*   Email    : 505544956@qq.com
*   Time     : 2017/08/24
"""

from spider import Spider
from database import DataBase

import re

class LibDownload(DataBase):
    def __init__(self, host, user, pwd, db):
        self.__tb = 'libdownload';
        DataBase.__init__(self, host, user, pwd, db)
        sql = 'CREATE TABLE IF NOT EXISTS ' + self.__tb + \
            ' (id INTEGER AUTO_INCREMENT,name TEXT, type TEXT,href TEXT,lang TEXT,type1 TEXT,type2 TEXT,path TEXT,download INT,date TIMESTAMP, PRIMARY KEY(id) ) \
            DEFAULT CHARSET=utf8;'
        DataBase.execute(self, sql)

        self.read(self.__on_read)
    def write(self, text):
        f = codecs.open(self.__tb + '.log', 'a', 'utf-8')
        f.write(text)
        f.close()
    def _select(self, conn, sql):
        cursor = conn.cursor()
        cursor.execute(sql)
        result = cursor.fetchall()
        cursor.close()
        return result
    def __parser_path(self, conn, info):
        def parser_liblist(url, info):
            info['lang'] = u'None'
            info['type1'] = u'None'
            info['type2'] = u'None'
            sql = "select id,name from liblist where href='" + url + "';"
            result1 = self._select(conn, sql)
            for r1 in result1:
                sql = "select id,name,type from libdir where href='" + r1[1] + "';"
                result2 = self._select(conn, sql)
                for r2 in result2:
                    if r2[2] == u'编程语言':
                        info['lang'] = r2[1]
                        if info['type1'] is u'None':
                            info['type1'] = r2[2]
                            info['type2'] = r2[1]
                        continue
                    info['type1'] = r2[2]
                    info['type2'] = r2[1]
        sql = "select id,name from libinfo where href='" + info['name'] + "';"
        result = self._select(conn, sql)
        for r in result:
            parser_liblist(r[1], info)
            break
    def __on_read(self, conn, args):
        sql = "select id,name,type,href from libstar;"
        result = self._select(conn, sql)
        length = len(result)
        for r in result:
            print('[LibDownload] id:%s/%d, type:%s, name:%s' % (r[0], length, r[2], r[1]))
            sql = "select id from " + self.__tb + " where name='%s'" % (r[1])
            result = self._select(conn, sql)
            if result:
                continue
            info = {'id': r[0], 'name': r[1], 'type': r[2], 'href': r[3]}
            self.__parser_path(conn, info)
            self.__download(info)
    def __on_save(self, conn, data, args):
        try:
            cursor = conn.cursor()
            sql = "INSERT INTO " + self.__tb + " (name,type,href,lang,type1,type2,path,download) VALUES(%s,%s,%s,%s,%s,%s,%s, 0);"
            cursor.execute(sql, (data['name'], data['type'], data['href'], data['lang'], data['type1'], data['type2'], data['path']) )
            cursor.close()
            conn.commit()
        except conn.Error, e:
            err = '[LibDownload] Error __on_save : %d %s, %s ' % (e[0], e[1], data)
            self.write(err + '\n')
            print(err)
        finally:
            print('[LibDownload] save id: %d, name: %s, data:%s' % (data['id'], data['name'], data))
    def __download(self, info):
        def getPath(t1, t2):
            path1 = re.sub(r'/', '', t1)
            path2 = re.sub(r'/', '', t2)
            return path1 + u'/' + path2
        info['path'] = getPath(info['type1'], info['type2'])
        self.save(self.__on_save, info)
#------------
libdownload = LibDownload('localhost', 'dzlua', 'dzlua', 'spider')
libdownload.wait()
libdownload.close()